package org.wattdepot.cli;

import org.wattdepot.client.WattDepotClient;
import org.wattdepot.client.WattDepotClientException;

/**
 * This class handles the initial processing of list commands.
 * 
 * @author Dean Kim
 * 
 */
public class ListCommandCli {

  private ListPower listPowerCommand;
  private ListSensorData listSensorDataCommand;
  private static String LIST_ERROR_MESSAGE = "Invalid list command. Type Help for a list of"
      + " commands.";

  /**
   * Valid list command list.
   * 
   * @author Dean Kim
   */
  private enum ListCommand {
    SOURCES, SOURCE, SUMMARY, SENSORDATA, POWER, TOTAL
  }

  /**
   * ListCommandCli constructor.
   */
  public ListCommandCli() {
    super();

    listPowerCommand = new ListPower();
    listSensorDataCommand = new ListSensorData();
    // TODO Auto-generated constructor stub
  }

  /**
   * Verifies that the list command is valid.
   * 
   * @param inputCommand Command to verify.
   * @return If the command is valid, boolean true is returned, otherwise false is returned.
   */
  protected boolean isValidCommand(String inputCommand) {
    boolean validCommand = false;
    ListCommand[] validCommands = ListCommand.values();

    for (ListCommand c : validCommands) {
      if (c.name().equals(inputCommand)) {
        validCommand = true;
      }
    }
    return validCommand;
  }

  /**
   * Processes the list command.
   * 
   * @param client Valid WattDepotClient.
   * @param inputCommand Tokenized list command with arguments.
   * @return Output of the list command.
   * @throws WattDepotClientException Throws WattDepotClientException.
   */
  protected String processCommand(WattDepotClient client, String[] inputCommand)
      throws WattDepotClientException {
    String returnString = null;

    if (inputCommand.length == 1) {
      returnString = ListCommandCli.LIST_ERROR_MESSAGE;
    }
    else {
      if (this.isValidCommand(inputCommand[1])) {
        
        switch (ListCommand.valueOf(inputCommand[1])) {
        case SENSORDATA:
          returnString = listSensorDataCommand.processCommand(client, inputCommand);
          break;
        case POWER:
          returnString = listPowerCommand.processCommand(client, inputCommand);
          break;
        default:
          returnString = ListCommandCli.LIST_ERROR_MESSAGE;
          break;
        } // end switch
      } // end if (this.isValidCommand(inputCommand[1]))
    } // end else
    return returnString;
  }

}
